package com.ruan.controller;

import com.ruan.common.Code;
import com.ruan.common.R;
import com.ruan.common.Result;
import com.ruan.pojo.Participant;
import com.ruan.service.ParticipantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/participant")
public class ParticipantController {
    @Autowired
    private ParticipantService participantService;

    //新增项目参与员工的名单@RequestBody Participant participant
    @PostMapping("/saveParticipantAssignmentInfo")
    public Result save(@RequestBody Map<String, Object> params){
        System.out.println("participant的参数有"+params);
//        log.info("新增项目参与员工的名单，项目参与员工的名单信息：{}",projectId,employeeId);//participant.toString()
        Long projectId = Long.parseLong(params.get("projectId").toString());
        List<String> employeeIdStrings = (List<String>)params.get("employeeId");//(List<Long>)

        List<Long> employeeIds = employeeIdStrings.stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());

        for (int i = 0; i < employeeIds.size(); i++) {
            Long employeeId = employeeIds.get(i);
            System.out.println("看看遍历的参数"+employeeIds);
            Participant participant = new Participant();
            participant.setProjectId(projectId);
            participant.setEmployeeId(employeeId);
            //把这个对象存进去
            participantService.save(participant);//participant
        }
//save方法继承自mybatis-plus的父接口IService（看看application.yml文件）,可以直接调用
        return new Result(Code.SAVE_OK,"新增项目参与员工名单成功");
    }
}
